﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using WcfServiceLibrary1.Ctrl;
using CinemaProject;

namespace WcfServiceLibrary1
{
    public partial class Cinema : Form
    {
        private IUserCtrl userCtrl;
        private IMovieCtrl movieCtrl;

        private List<User> userList;
        private List<Movie> movieList;
        private List<User> reservationUserList;

        public Cinema()
        {
            InitializeComponent();

            txtSearch.Text = "Søg efter medlem vha. Navn";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                lblMembers.Items.Clear();
                userList.Clear();

                userList = userCtrl.getUsersSearch(txtSearch.Text);

                foreach (User item in userList)
	            {
                    lblMembers.Items.Add(item._name);
	            }

                lblResult.Text = "";
            }
            catch (Exception)
            {
                lblResult.Text = "Der opstod en fejl.";
            }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                userCtrl.insertUser(new User(txtUserEmail.Text, txtUserName.Text, chkSeniorDiscount.Checked, chk10.Checked, chkStudierabat.Checked));

                lblResult.Text = "Medlem gemt.";
            }
            catch (Exception)
            {
                lblResult.Text = "Der opstod en fejl.";
            }
        }

        private void lblMembers_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                User user = userList[lblMembers.SelectedIndex];

                txtUserEmail.Text = user._email;
                txtUserName.Text = user._name;

                chk10.Checked = user._filmRabat;
                chkSeniorDiscount.Checked = user._seniorRabat;
                chkStudierabat.Checked = user._studieRabat;
            }
            catch (Exception)
            {
                lblResult.Text = "Der opstod en fejl.";
            }
        }

        private void btnMovieInsert_Click(object sender, EventArgs e)
        {
            try
            {
                movieCtrl.insertMovie(new Movie(txtMovieTitle.Text, txtMovieDescription.Text, txtMovieLength.Text, txtMovieReview.Text, txtMovieDirector.Text, txtMovieGenre.Text));

                lblMovieResult.Text = "Film oprettet.";
            }
            catch (Exception)
            {
                lblMovieResult.Text = "Der opstod en fejl.";
            }
        }

        private void btnMovieUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                movieCtrl.updateMovie(new Movie(movieList[lblMoviesSearch.SelectedIndex]._id, txtMovieTitle.Text, txtMovieDescription.Text, txtMovieLength.Text, txtMovieReview.Text, txtMovieDirector.Text, txtMovieGenre.Text));

                movieList[lblMoviesSearch.SelectedIndex]._title = txtMovieTitle.Text;

                lblMovieResult.Text = "Film redigeret.";
            }
            catch (Exception)
            {
                lblMovieResult.Text = "Der opstod en fejl.";
            }
        }

        private void btnMovieSearch_Click(object sender, EventArgs e)
        {
            try
            {
                lblMoviesSearch.Items.Clear();
                movieList.Clear();

                movieList = movieCtrl.getMovieSearch(txtMovieSearch.Text);

                foreach (Movie item in movieList)
                {
                    lblMembers.Items.Add(item._title);
                }

                lblMovieResult.Text = "";
            }
            catch (Exception)
            {
                lblMovieResult.Text = "Der opstod en fejl.";
            }
        }

        private void btnMovieDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int selectedIndex = lblMoviesSearch.SelectedIndex;

                movieCtrl.deleteMovie(movieList[selectedIndex]._id);
                movieList.RemoveAt(selectedIndex);
                lblMoviesSearch.Items.RemoveAt(selectedIndex);

                lblMoviesSearch.SelectedIndex = -1;

                lblMovieResult.Text = "Film fjernet.";
            }
            catch (Exception)
            {
                lblMovieResult.Text = "Der opstod en fejl.";
            }
        }

        private void btnReservationSearch_Click(object sender, EventArgs e)
        {
            try
            {
                lblReservationMembers.Items.Clear();
                reservationUserList.Clear();

                reservationUserList = userCtrl.getUsersSearch(txtReservationMemberSearch.Text);

                foreach (User item in reservationUserList)
                {
                    lblReservationMembers.Items.Add(item._name);
                }

                lblReservationResult.Text = "";
            }
            catch (Exception)
            {
                lblReservationResult.Text = "Der opstod en fejl.";
            }
        }
    }
}
